package tgc.edu.examxy.web.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.examxy.custom.DataGridParam;
import tgc.edu.examxy.custom.DataGridUtils;
import tgc.edu.examxy.entity.CurriculumPlan;
import tgc.edu.examxy.entity.Student;
import tgc.edu.examxy.entity.StudentInformation;
import tgc.edu.examxy.entity.SysFile;
import tgc.edu.examxy.entity.TbCurriculum;
import tgc.edu.examxy.entity.VideoCourseware;
import tgc.edu.examxy.security.User2;
import tgc.edu.examxy.service.CurriculumPlanService;
import tgc.edu.examxy.service.StudentService;
import tgc.edu.examxy.service.SysFileService;
import tgc.edu.examxy.service.TbCurriculumService;
import tgc.edu.examxy.service.VideoCoursewareService;
import tgc.edu.examxy.web.form.StudentInformationForm;
/****
 * 学习园地
 * @author 936642284
 *
 */
@Controller
@RequestMapping(value="/fileresources")
public class FileresourcesController {
	@Autowired
	private SysFileService fileservice;
	@Autowired
	private StudentService studentservice;
	@Autowired
	private TbCurriculumService tbservice;
	@Autowired
	private VideoCoursewareService vservice;
	@Autowired
	private CurriculumPlanService planservice;
	
	@RequestMapping(value = "/manage")
	private String manage() {
		return "fileresources/manage";
	}
	
	@RequestMapping(value = "/page")
	@ResponseBody
	private Object page(DataGridParam param,String search) {
		Sort sort = Sort.by("createDate");
		Pageable pageable = param.getPageable(sort);
		Specification<VideoCourseware> spec = buildSpec(search);
		Page<VideoCourseware> page = vservice.findAll(spec, pageable);
		return DataGridUtils.buildResult(page);
	}

	private Specification<VideoCourseware> buildSpec(String search) {
		
		Specification<VideoCourseware> spe = new Specification<VideoCourseware>() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<VideoCourseware> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				SecurityContext ctx = SecurityContextHolder.getContext();
			    Authentication auth = ctx.getAuthentication();
			    User2 user = (User2) auth.getPrincipal();
			    Student usernames = studentservice.findByUsername(user.getUsername());
			    if(usernames==null) {
			    	return null;
			    }
			    Predicate namesss = cb.equal(root.get("tbclass"), usernames.getTbClass());
				rules.add(namesss);
				Predicate namess = cb.equal(root.get("isstart"),true);
				rules.add(namess);
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		Specification<VideoCourseware> specification = new Specification<VideoCourseware>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<VideoCourseware> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				Predicate namess = cb.like(root.get("sysfile").get("filetypename"), "%课件资源%");
				rules.add(namess);
				
				return cb.or(rules.toArray(new Predicate[rules.size()]));
			}
		};
		if(StringUtils.hasText(search)&&search!=""&&search!=null) {
			Specification<VideoCourseware> specificationc = new Specification<VideoCourseware>() {

				private static final long serialVersionUID = 1L;
				
				@Override
				public Predicate toPredicate(Root<VideoCourseware> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					HashSet<Predicate> rules=new HashSet<>();
					if(StringUtils.hasText(search)) {
						rules.add(cb.like(root.get("sysfile").get("tbCurriculum").get("courseName"), "%"+search+"%"));
					}
					if(StringUtils.hasText(search)) {
						rules.add(cb.like(root.get("sysfile").get("filename"), "%"+search+"%"));
					}
					if(StringUtils.hasText(search)) {
						rules.add(cb.like(root.get("sysfile").get("resourceprofile"), "%"+search+"%"));
					}
					return cb.or(rules.toArray(new Predicate[rules.size()]));
				}
			};
			return spe.and(specification).and(specificationc);
		}else {
			return spe.and(specification);
		}
	}
	@RequestMapping(value="/voide")
	private String voide(String uuid,ModelMap map) {
		map.put("uuid", uuid);
		
		return "fileresources/voide";
	}
	/****
	 * 打开显示课程
	 * @return
	 *//*
	@RequestMapping(value="/curriculum")
	private String  curriculum() {
		return "fileresources/curriculum";
	}
	@RequestMapping(value="/curriculumpage")
	@ResponseBody
	private Object curriculumpage(DataGridParam param) {
		Sort sort = Sort.by("createDate");
		Pageable pageable = param.getPageable(sort);
		Specification<CurriculumPlan> spec = buildSpecplan();
		Page<CurriculumPlan> plan = null;
		if(spec!=null) {
			plan = planservice.findAll(spec,pageable);
			return DataGridUtils.buildResult(plan);
		}
		return DataGridUtils.buildResult(plan);
	}

	private Specification<CurriculumPlan> buildSpecplan() {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    Student usernames = studentservice.findByUsername(user.getUsername());
	    if(usernames==null) {
	    	return null;
	    }
		Specification<CurriculumPlan> spec =  new Specification<CurriculumPlan>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<CurriculumPlan> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				Predicate namess = cb.equal(root.get("tbClass"), usernames.getTbClass());
				rules.add(namess);
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}*/

	
}
